package b4.view.component.smithy
{
	import b4.config.BasicObjConfig;
	import b4.config.Config;
	import b4.model.vo.player.BasicObj;
	import b4.model.vo.player.Equip;
	import b4.util.BFont;
	import b4.view.component.basic.TitledPanel;
	import b4.view.component.events.ItemClickEvent;
	import b4.view.component.pack.BasicObjItem;
	
	import flash.text.TextFormat;
	
	import org.aswing.BorderLayout;
	import org.aswing.JButton;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	
	public class EquipStren extends BaseSmithy
	{
		public static const  strengthenRange:Object = {10000:[1, 4], 10001:[5, 8], 10002:[9, 12]};
		public static const  luckyUseRange:Object = {10003:[1, 4], 10004:[5, 8], 10005:[9, 12]};
		public static const  protectUseRange:Object = {10006:[1, 4], 10007:[5, 8], 10008:[9, 12]};	
		public static const  upgrade:Object = {10003: 5, 10004: 10, 10005: 15};
		public static const  successRate:Array = [100, 100, 60, 40, 30, 20, 10, 10, 5, 5, 1, 1];
		public var equipBox:BoxObj;
		public var stenStoneBox:BoxObj;
		private var luckyBox_1:BoxObj;
		private var luckyBox_2:BoxObj;
		private var luckyBox_3:BoxObj;
		private var luckyBox_4:BoxObj;
		private var luckyBox_5:BoxObj;
		private var protectStoneBox:BoxObj;
		private var equipPreView:JLabel;
		private var retriveButton:JButton;
		private var payPane:TitledPanel;
		private var succPane:TitledPanel;
		private var succRate:JTextField;
		private var failAppend:TitledPanel;
		private var targetLevel:int;
		public var strengthenBtn:JButton;
		private var payTxt:JTextField;
		
		public function EquipStren()
		{
			super("请放入要强化的装备","强化说明");
			var textFormat:TextFormat = new TextFormat(BFont.DEFAULT_FONT, 12, null, true);
			var leftTopContentNorth:JPanel = new JPanel();
			leftTopContent.append(leftTopContentNorth, BorderLayout.CENTER);
			tryToLoadRs = {1:"请先放入要强化的装备！", 2:"请放入合适的强化石！",
							3:"请放入合适的幸运符！", 4:"请放入合适的保护符！"};
			targetLevel = 0;
			
			equipBox = new BoxObj("装备", 1, 1028);
			boxArr.push(equipBox);
			
			leftTopContentNorth.append(equipBox);

			equipPreView = new JLabel("极品预览");
			retriveButton = new JButton("取回装备");
			
			leftTopContentNorth.appendAll(equipPreView,  retriveButton);

			luckyBox_1 = new BoxObj("幸运符", 10003, 10005);
			luckyBox_2 = new BoxObj("幸运符", 10003, 10005);
			luckyBox_3 = new BoxObj("幸运符", 10003, 10005);
			luckyBox_4 = new BoxObj("幸运符", 10003, 10005);
			luckyBox_5 = new BoxObj("幸运符", 10003, 10005);
			boxArr.push(luckyBox_1,luckyBox_2,luckyBox_3, luckyBox_4, luckyBox_5);
			leftTopContentNorth.appendAll(luckyBox_1, luckyBox_2, luckyBox_3, luckyBox_4, luckyBox_5);
			
			stenStoneBox = new BoxObj("强化石", 10000, 10002);
			protectStoneBox = new BoxObj("保护符", 10006, 10008);
			boxArr.push(stenStoneBox, protectStoneBox);
			leftTopContentNorth.appendAll(stenStoneBox, protectStoneBox);

			payPane = new TitledPanel("费用（铜币）", 75, 55);
			payTxt = new JTextField();
			
			payTxt.setPreferredHeight(20);
			payTxt.setPreferredWidth(75);
			payPane.append(payTxt);

			succPane = new TitledPanel("总成功率", 75, 55);
			succRate = new JTextField();
			succRate.setPreferredHeight(20);
			succRate.setPreferredWidth(75);
			
			succPane.append(succRate);
			
			failAppend = new TitledPanel("失败附加", 75, 25);
			leftTopContentNorth.appendAll(payPane,succPane, failAppend);
			
			textArea.setText("装备强化的说明");
			strengthenBtn = new JButton("开始强化");
			
			payTxt.setDefaultTextFormat(textFormat);
			textArea.setDefaultTextFormat(textFormat);
			
			leftBottom.append(strengthenBtn);

			equipBox..addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			equipBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			luckyBox_1.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			luckyBox_2.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			luckyBox_3.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			luckyBox_4.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			luckyBox_5.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			stenStoneBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			protectStoneBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
		}
		
		private function onLoadItemOff(e:ItemClickEvent):void
		{
			var boxObj:BasicObj = e.data as BasicObj;
			var objType:int;
			if (boxObj is Equip)
			{
				targetLevel = 0;
				super.loadOffAll();
			}
			refreshRate();
			refreshPay();
			var tabLoadOffEvent:ItemClickEvent = new ItemClickEvent(ItemClickEvent.TAB_LOAD_ITEM_OFF)
			dispatchEvent(tabLoadOffEvent);
		}
		
		override public function loadOffAll():void
		{
			super.loadOffAll();
			targetLevel = 0;
			refreshPay();
			refreshRate();
		}
		
		private function refreshPay():void
		{
			if (targetLevel > 0)
				payTxt.setText(String(targetLevel*100));
			else
				payTxt.setText("");
		}
		
		override public function tryToLoadItem(obj:BasicObj):int
		{
			var itemConfig:BasicObjConfig = Config.objConfigs[obj.index];
			var objType:int = itemConfig.type;
			var rc:int = 0;
			
			if ((objType != BasicObjConfig.TYPE_EQUIP) && equipBox.hasObj == false)
			{
				rc = 1;
			}
			else if ((objType != BasicObjConfig.TYPE_EQUIP) && (obj.index >= 10000 && obj.index <= 10002) && 
									(targetLevel < strengthenRange[obj.index][0] || targetLevel > strengthenRange[obj.index][1]))
			{
				rc = 2;
			}
			else if ((objType != BasicObjConfig.TYPE_EQUIP) && (obj.index >= 10003 && obj.index <= 10005) && 
				(targetLevel < luckyUseRange[obj.index][0] || targetLevel > luckyUseRange[obj.index][1]))
			{
				rc = 3;
			}
			else if ((objType != BasicObjConfig.TYPE_EQUIP) && (obj.index >= 10006 && obj.index <= 10008) && 
				(targetLevel < protectUseRange[obj.index][0] || targetLevel > protectUseRange[obj.index][1]))
			{
				rc = 4;
			}
			return rc;
		}
		
		override public function loadItem(obj:BasicObj):BoxObj
		{
			var returnBox:BoxObj = super.loadItem(obj);
			if (returnBox!=null && returnBox.boxItem.obj is Equip)
			{
				var curEquip:Equip = obj as Equip;
				targetLevel = curEquip.strengthen + 1;
			}
			refreshRate();
			refreshPay();
			return returnBox;
		}
		
		private function refreshRate():void
		{
			//		（基本成功率 + 幸运符成功率）*（1 + 帮会加成） + VIP成功率
			var rate:int;
			if (equipBox.hasObj)
			{
				var baseRate:int = successRate[targetLevel];
				var luckyRate:int = getLuckyRate();
				rate = baseRate + luckyRate;
				succRate.setText(rate + "%");
			}
			else
			{
				succRate.setText("");
			}
//			succRate.setText("");
		}
		
		private function getLuckyRate():int
		{
			var luckBoxBuff :Array = [luckyBox_1, luckyBox_2, luckyBox_3, luckyBox_4, luckyBox_5];
			var rate:int = 0;
			for (var indexstr:String in luckBoxBuff)
			{
				var luckyBox:BoxObj =  luckBoxBuff[indexstr] as BoxObj;
				if (luckyBox.hasObj)
				{
					var boxItem:BasicObjItem = luckyBox.boxItem as BasicObjItem;
					
					rate += upgrade[boxItem.obj.index]
				}
			}
			return rate;
		}		
	}
	
}
